package com.rf.richfitwheel.admin.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.SysCurInfoMapper;
import com.rf.richfitwheel.admin.sys.model.SysCurInfo;
import com.rf.richfitwheel.admin.sys.model.SysCurInfoExample;
import com.rf.richfitwheel.admin.sys.service.SysCurInfoService;
import com.rf.richfitwheel.common.exception.BusinessException;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.StringUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service("sysCurInfoService")
public class SysCurInfoServiceImpl implements SysCurInfoService {
    @Autowired
    private SysCurInfoMapper sysCurInfoMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<SysCurInfo> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        SysCurInfo sysCurInfo = (SysCurInfo) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), SysCurInfo.class);
        SysCurInfoExample example = new SysCurInfoExample();
        SysCurInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(sysCurInfo.getCurco())){
            criteria.andCurcoLike("%"+sysCurInfo.getCurco()+"%");
        }
        if (StringUtils.isNotBlank(sysCurInfo.getLocalName())){
            criteria.andLocalNameLike("%"+sysCurInfo.getLocalName()+"%");
        }
        example.setOrderByClause("curco");
        List<SysCurInfo> list = sysCurInfoMapper.selectByExample(example);
        pageUtils = new PageUtils(p);
        return pageUtils;
    }

    @Override
    public void save(SysCurInfo sysCurInfo) throws Exception{
        SysCurInfoExample example = new SysCurInfoExample();
        SysCurInfoExample.Criteria criteria = example.createCriteria();
        criteria.andCurcoEqualTo(sysCurInfo.getCurco());
        List<SysCurInfo> list = sysCurInfoMapper.selectByExample(example);
        if(!list.isEmpty()){
            throw new BusinessException("币种已被使用");
        }
        sysCurInfo.setId(UuidUtil.get32UUID());
        sysCurInfoMapper.insert(sysCurInfo);
    }

    @Override
    public void update(SysCurInfo sysCurInfo) throws Exception {
        SysCurInfoExample example = new SysCurInfoExample();
        SysCurInfoExample.Criteria criteria = example.createCriteria();
        criteria.andCurcoEqualTo(sysCurInfo.getCurco());
        criteria.andIdNotEqualTo(sysCurInfo.getId());
        List<SysCurInfo> list = sysCurInfoMapper.selectByExample(example);
        if(!list.isEmpty()){
            throw new BusinessException("币种已被使用");
        }
        sysCurInfoMapper.updateByPrimaryKeySelective(sysCurInfo);
    }

    @Override
    public void deleteBatch(String[] ids) {
        for (String id : ids) {
            sysCurInfoMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public SysCurInfo selectById(String id) {
        return sysCurInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public BigDecimal getConvertAmtByCurNo(BigDecimal amt, String cur) {
        if (cur.equals("CNY")) {
            return amt;
        }
        SysCurInfoExample example = new SysCurInfoExample();
        SysCurInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(cur)){
            criteria.andCurcoEqualTo(cur);
        }
        List<SysCurInfo> list = sysCurInfoMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            BigDecimal rate = list.get(0).getExrate();
            BigDecimal amtCNY = amt.multiply(rate);
            return amtCNY;
        }
        return amt;
    }

    @Override
    public List<SysCurInfo> getCurList() {
        SysCurInfoExample example = new SysCurInfoExample();
        SysCurInfoExample.Criteria criteria = example.createCriteria();
        List<SysCurInfo> list = sysCurInfoMapper.selectByExample(example);
        return list;
    }
}
